package com.sskj.gcc.ui.activity;

import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;

import com.bulong.rudeness.RudenessScreenHelper;
import com.hey.lib.HeySpinner;
import com.sskj.gcc.R;
import com.sskj.gcc.presenter.BillingDetailsActivityPresenter;
import com.sskj.gocoin.common.adapter.BaseAdapter;
import com.sskj.gocoin.common.adapter.ViewHolder;
import com.sskj.gocoin.common.decoration.DividerLineItemDecoration;
import com.sskj.gocoin.common.widget.ToolBarLayout;
import com.sskj.gocoin.m.bean.AseetListBean;
import com.sskj.gocoin.m.bean.CapitalDetailData;
import com.sskj.gocoin.m.bean.ChargeCoinItemBean;
import com.sskj.gocoin.m.bean.RechargeListBean;
import com.sskj.gocoin.m.bean.ZiJinData;
import com.sskj.gocoin.utils.DateUtil;
import com.sskj.gocoin.v.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;

public class BillingDetailsActivity extends BaseActivity<BillingDetailsActivityPresenter> {

    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.spinner)
    HeySpinner spinner;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private List<ChargeCoinItemBean> assetBeans;
    private String assetName;
    private int assetId;
    private BaseAdapter<ZiJinData.DataBean> mAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_billing_details;
    }

    @Override
    public BillingDetailsActivityPresenter getPresenter() {
        return new BillingDetailsActivityPresenter();
    }

    @Override
    protected void initView() {

        toolbar.setLeftButtonOnClickLinster(view -> finish());

        mPresenter.requestUserAssetList();

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setFirstDraw(false)
                .setLastDraw(false)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(this, 2))
                .setDividerColor(ContextCompat.getColor(this, com.sskj.gocoin.R.color.app_divider)));
        mAdapter = new BaseAdapter<ZiJinData.DataBean>(R.layout.item_capital_details_asset, null, recyclerView) {
            @Override
            public void bind(ViewHolder holder, ZiJinData.DataBean item) {
                holder.setText(R.id.item_capital_asset_count, item.getMoney() + " " + assetName);
                holder.setText(R.id.item_capital_asset_time, item.getCreated_at());
                holder.setText(R.id.item_capital_asset_remark, TextUtils.isEmpty(item.getMark()) ? item.getPtype() : item.getMark());
            }
        };
        mAdapter.setEmtyView();
    }

    private void initSpinner() {
        List<String> data = new ArrayList<>();
        for (ChargeCoinItemBean bean : assetBeans) {
            data.add(bean.getCode());
        }
        spinner.attachData(data);
        spinner.setOnSelectListener(position -> {
            assetName = assetBeans.get(position).getCode();
            assetId = assetBeans.get(position).getId();
            mPresenter.getAssetRecordList(assetId);
        });
        spinner.setSelectIndex(0);
        mPresenter.getAssetRecordList(assetBeans.get(0).getId());
    }

    public void onDataSuccess(List<ChargeCoinItemBean> data) {

        Flowable.fromIterable(data)
                .filter(aseetBean -> !aseetBean.getCode().equals("GCT"))
                .toList()
                .subscribe(aseetBeans -> {
                    assetBeans = aseetBeans;
                    initSpinner();
                });
    }

    public void onAssetRecordSuccess(List<ZiJinData.DataBean> res) {
        mAdapter.setNewData(res);
        mAdapter.notifyDataSetChanged();
    }

}
